package com.njitzx.task;

import com.njitzx.entity.po.Order;
import com.njitzx.mapper.OrderGoodsMapper;
import com.njitzx.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class OrderTask {

    @Autowired
    OrderMapper orderMapper;

    @Scheduled(cron = "0 0 1 * * ?") // 每隔2秒执行一次
    public void harvestCheck() {
        System.out.println("开始执行自动收货");
        //拿到所有的未支付订单
        List<Order> collect = orderMapper.select(new Order()).stream()
                        .filter(item -> item.getStatus() == 2).collect(Collectors.toList());
        for (Order order : collect) {
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime createTime = order.getCreateTime();
            long minutesBetween = ChronoUnit.DAYS.between(createTime, now);
            if (minutesBetween >7) {
                orderMapper.updateStatus(order.getOrderId());
            }
        }
    }
    @Scheduled(cron = "0 0/1 * * * ?") // 每隔2秒执行一次
    public void OrderCheck() {
        System.out.println("开始执行 15分钟超时订单");
        //拿到所有的未支付订单
        List<Order> collect = orderMapper.select(new Order()).stream().filter(item -> item.getStatus() == 0).collect(Collectors.toList());
        for (Order order : collect) {
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime createTime = order.getCreateTime();
            long minutesBetween = ChronoUnit.MINUTES.between(createTime, now);
            if (minutesBetween > 15) {
                orderMapper.updateharvestState(order.getOrderId());

            }
        }
    }
}
